package my.syncfilter.entity;


import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

public enum MemoryDataStore
{
    INSTANCE;

    private static final Map<String, MonitorData> monitorDataMaps = new ConcurrentHashMap<String, MonitorData>(8);

    private static final Map<String, BillConfig> billConfigMaps = new ConcurrentHashMap<String, BillConfig>(8);

    public MonitorData getMonitorData(String prefix)
    {
        return monitorDataMaps.get(prefix);
    }



    public boolean addUnit(String prefix, Properties properties)
    {
        if(monitorDataMaps.containsKey(prefix) || billConfigMaps.containsKey(prefix)){
            return false;
        }
        BillConfig billConfig = BillConfig.factory(prefix, properties);
        MemoryDataStore.INSTANCE.putBillConfig(prefix, billConfig);
        MonitorData monitorData = new MonitorData(Integer.parseInt(billConfig.getChannelCapacity()));
        //设置未启动状态
        monitorData.setStatus(0);
        MemoryDataStore.INSTANCE.putMonitorData(billConfig.getPrefix(), monitorData);
        return true;
    }

    public boolean release(String prefix){
        monitorDataMaps.remove(prefix);
        billConfigMaps.remove(prefix);
        return true;
    }


    public boolean putMonitorData(String prefix,MonitorData monitorData)
    {
        if(monitorDataMaps.putIfAbsent(prefix, monitorData) != null) {
        }
        return true;
    }

    public boolean putMonitorDataCover(String prefix,MonitorData monitorData)
    {
        monitorDataMaps.put(prefix,monitorData);
        return true;
    }


    public BillConfig getBillConfig(String prefix)
    {
        return billConfigMaps.get(prefix);
    }

    public boolean putBillConfig(String prefix,BillConfig billConfig)
    {
        if(billConfigMaps.putIfAbsent(prefix, billConfig) != null) {
        }
        return true;
    }

    public static Map<String, MonitorData> getMonitorDataMaps() {
        return monitorDataMaps;
    }
}
